/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.session;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import jpa.entities.Course;
import jpa.entities.StudentCourse;
import jpa.entities.StudentCoursePK;
import jpa.entities.Students;

/**
 *
 * @author KenShin
 */
@Stateless
public class StudentCourseFacade extends AbstractFacade<StudentCourse> {
    @PersistenceContext(unitName = "SupportiveLearning-ejbPU")
    private EntityManager em;
    private StudentCourse studentcourse;
    private Students student;
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public StudentCourseFacade() {
        super(StudentCourse.class);
    }
    public StudentCourse addStudentCourseValid(Students student,Course course) {
        studentcourse = new StudentCourse();
        studentcourse.setStudents(student);
        studentcourse.setCourse(course);
        StudentCoursePK spk = new StudentCoursePK();
        spk.setCourseId(course.getCourseId());
        spk.setStudentId(student.getStudentId());
        studentcourse.setStudentCoursePK(spk);
        return studentcourse;
    }
    public StudentCourse checkStudentCourseValid(String UserName,String CourseName)
    {
        List<StudentCourse> results = em.createQuery("SELECT s FROM StudentCourse s WHERE s.students.userName = :userName AND s.course.courseName = :courseName")
           .setParameter("userName", UserName)
           .setParameter("courseName", CourseName)
           .getResultList();
         
         if (!results.isEmpty() ) {
           studentcourse = results.get(0);
        }
        return studentcourse;
    }
    public List<StudentCourse> GetListAssignmentOfStudent(String UserName)
    {
        List<StudentCourse> results = em.createQuery("SELECT s FROM StudentCourse s WHERE s.students.userName = :userName")
           .setParameter("userName", UserName)
           .getResultList();
        return (List<StudentCourse>) results; 
    }
    public List<StudentCourse> SearchCourseByName(String courseName)
    {
        List<StudentCourse> results = em.createQuery("SELECT s FROM StudentCourse s WHERE s.course.courseName like :courseName order by s.course.courseId desc")
           .setParameter("courseName","%"+ courseName +"%")
           .getResultList();
        return (List<StudentCourse>) results; 
    }
    public List<StudentCourse> GetListAssignmentOfStudentByCourseName(Course course)
    {
        List<StudentCourse> results = em.createQuery("SELECT s FROM StudentCourse s WHERE s.course = :course")
           .setParameter("course", course)
           .getResultList();
        return (List<StudentCourse>) results; 
    }
}